//
//  XJZPQSearchWYViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  宅配圈搜索网易效果

#import "XJZPQSearchWYViewController.h"
#import "XJZPQSearchGoodsViewController.h" //搜索商品
#import "XJZPQSearchShopViewController.h" //搜索店铺

#import "RBSearchNaviBar.h"


@interface XJZPQSearchWYViewController ()<YNPageViewControllerDelegate,YNPageViewControllerDataSource>
@property (nonatomic, strong) RBSearchNaviBar *searchBarView;


@end

@implementation XJZPQSearchWYViewController

#pragma mark -  一 创建对象+配置
+ (instancetype)suspendXJZPQSearchVYView {
    YNPageConfigration *configure = [[YNPageConfigration alloc] init];
    configure.selectedItemFont = XJFONTSET_M(16.0);
    configure.itemFont = XJFONTSET_R(16.0);
    
    configure.scrollMenu = NO;
    configure.aligmentModeCenter = NO;
    
    configure.lineWidthEqualFontWidth = YES;
    
    XJZPQSearchWYViewController *vc = [XJZPQSearchWYViewController pageViewControllerWithControllers:[self vcArrays] titles:[self titlesArr] config:configure];
    

    vc.delegate = vc;
    vc.dataSource = vc;
    vc.pageIndex = 0;
    
    
    return vc;
}

+ (NSArray *)vcArrays {
    XJZPQSearchGoodsViewController *searchGoodsVC = [[XJZPQSearchGoodsViewController alloc] init];
    
    XJLog(@"3333:%p",searchGoodsVC);
    
    XJZPQSearchShopViewController *searchShopsVC = [[XJZPQSearchShopViewController alloc] init];
    
    return @[searchGoodsVC,searchShopsVC];
}

+ (NSArray *)titlesArr {
    return @[@"搜索商品",@"搜索店铺"];
}

#pragma mark - YNPageViewControllerDataSource
- (UIScrollView *)pageViewController:(YNPageViewController *)pageViewController pageForIndex:(NSInteger)index {
    UIViewController *vc = pageViewController.controllersM[index];
    if ([vc isKindOfClass:[XJZPQSearchShopViewController class]]) {
        
        return [(XJZPQSearchShopViewController *)vc collectionView];
    }else {
       
        return [(XJZPQSearchGoodsViewController *)vc collectionView];
    }
}

#pragma mark - YNPageViewControllerDelegate
- (void)pageViewController:(YNPageViewController *)pageViewController
            contentOffsetY:(CGFloat)contentOffset
                  progress:(CGFloat)progress {
    //        NSLog(@"--- contentOffset = %f,    progress = %f", contentOffset, progress);
}

#pragma mark - 二 lazy
- (RBSearchNaviBar *)searchBarView {
    if (!_searchBarView) {
        
        _searchBarView = [[RBSearchNaviBar alloc] initWithFrame:CGRectMake(0, KStatusBarHeight, KWidth, 64)]; // Y轴:去掉状态栏。
        _searchBarView.placeholder = @"搜索内容";
        XJWeakSelf(self);
        _searchBarView.cancelBlock = ^{
            [weakself.navigationController popToRootViewControllerAnimated:YES];
        };
        
        _searchBarView.searchBlock = ^(NSString *searchStr) {
            XJLog(@"搜索内容是:%@",searchStr);
            
            //搜索内容为空不做操作。
            if (![NSString isEmptyString:searchStr]) {
                UIViewController *vc = weakself.controllersM[weakself.pageIndex];
                
                if ([vc isKindOfClass:[XJZPQSearchShopViewController class]]) {
                    XJLog(@"搜索店铺");
                    
                    [(XJZPQSearchShopViewController *)vc requestDataWithKeyWord:searchStr];
                }else {
                    
                    [(XJZPQSearchGoodsViewController *)vc requestDataWithKeyWord:searchStr];
                    XJLog(@"搜索商品");
                }
            }
        };
        
    }
    return _searchBarView;
}


#pragma mark - 三 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.titleView = self.searchBarView;
}

#pragma mark - 四

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
